<template>
  <forms ref="forms" :config="config" @submit:forms="onsubmit" :btns="btns">
    <template #index="{row}">
      <p>第{{row.value}}期</p>
    </template>
    <template #dividend_config="{row}">
      <checkbox :config="level_map" :value="row.level_arr.join(',')" @change="change_level"></checkbox>
      <div class="flex">
        <label v-for="(v,i) in row.level_arr" style="width: 150px;">
          <span class="text-ellipsis">{{ level_map[v] }}</span>
          <el-input :disabled="i==row.level_arr.length-1" v-model="row.value_arr[v]" :name="row.name+'['+v+']'" type="number" @change="change_ratio($event,i)" style="width: 120px;">
            <template #append >%</template>
          </el-input>
        </label>
      </div>
    </template>
  </forms>
</template>

<script>
import {iframe} from "@/js/tool.js";
import {ajax} from "@/js/api_tool.js";

export default {
  components: {},

  props: ['__iframe'],
  data(){
    let id = this.$get('id'), btns = undefined;

    let not_dividend_action_map = this.admin_config('status_alias_map.log_dividend.not_dividend_action');
    let can_dividend_map = this.admin_config('status_alias_map.log_dividend.can_dividend');

    let forms_config = {
      index:{name:'index', label:'期数', type:'index'},
      pay_money_all:{name:'pay_money_all', label:'总业绩'},
      dividend_money_all:{name:'dividend_money_all', label:'总分红'},

      dividend_ratio:{name:'dividend_ratio', label:'分红比例', type:'text_one', config:'number', tips:'本期中，订单成交后，分红金额占支付金额的比例[0-99.99]'},
      // dividend_level:{name:'dividend_level', label:'分红等级', type:'checkbox', config:[], tips:'参与分红的用户等级'},
      dividend_config:{name:'dividend_config', label:'分红配置', type:'json_param_checkbox', config:[], tips:'配置每个等级的分红金额配比', value:'', onchange:this.change_dividend_config},
      pay_money_need:{name:'pay_money_need', label:'业绩需求', type:'text_one', config:'number', tips:'分红模式为【默认】时，分红池业绩大于业绩需求才会分红，否则不分红。其他模式无效'},
      can_dividend:{name:'can_dividend', label:'分红模式', type:'radio', config:can_dividend_map, tips:'一个月结束后，判断是否分红的模式'},
      not_dividend_action:{name:'not_dividend_action', label:'未分红的处理', type:'radio', config:not_dividend_action_map, tips:'如果当期不分红，那么多对于当期累加的分红金额和业绩的处理'},
    }
    if (id){
      forms_config.create_time = {label:'创建时间',name:'create_time',tips:''};
    }
    if (this.$get('is_info')){
      s94Mjs.each(forms_config, function (row){ row.is_info = true; });
      btns = [];
    }
    return {
      id: id,
      btns: btns,
      config: forms_config,

      level_map: {},
      change_ing: false,
    }
  },
  methods: {
    onsubmit(formData){
      if (this.change_ing) return this.tips("配置变更过中，请重试!");
      let url = this.id ? `/admin/log_dividend/edit?id=`+this.id : '/admin/log_score/add';
      ajax({url:url, method:'POST', data:formData}, (res)=>{
        if (this.__iframe){
          iframe.exe('log_dividend/index', '$refs.list_url.list');
          iframe.hide('log_dividend/edit');
          if (!this.id) this.$refs['forms'].init();
        }else {
          this.$router.push('index');
        }
      }, (res)=>{
        if (res.errors) this.$refs['forms'].errors = res.errors;
      })
    },
    change_dividend_config(row, v){
      let value_arr = JSON.parse(v);
      let all = 0;
      let ks = Object.keys(value_arr);
      ks.forEach((k,i)=>{
        let v = Number(value_arr[k]);
        v = all+v > 100 ? 0 : v;
        value_arr[k] = v;
        all += v;
      });
      this.change_ing = true;
      setTimeout(()=>{
        this.change_ing = false;
        this.config.dividend_config.value = JSON.stringify(value_arr);
      },50);
    },
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;
    if (this.id) {
      ajax({url:'/admin/log_dividend/info?id='+this.id, cache:true}, (res)=>{
        this.$refs['forms'].init(res.data);
        return false;
      }, ()=>{
        if (this.__iframe){
          iframe.remove('edit');
        }else {
          this.$router.push('index');
        }
      })
    }else {
      if (this.__iframe){
        iframe.remove('edit');
      }else {
        this.$router.push('index');
      }
    }

    //其他初始化操作
    ajax({url:'/admin/log_dividend/config_map', cache:true}, ({data})=>{
      this.level_map = data.level_map;
      this.config.dividend_config.config = {
        key_options: data.level_map,
        input: {type:"text_one", config:"number", inline:1, append:"%"},
      }

      // this.config.dividend_level.config = data.level_map;
    })

  }
}
</script>
